package com.guiji.quartz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.guiji.common.core.mybatisplus.core.ServicePlusImpl;
import com.guiji.common.core.page.PagePlus;
import com.guiji.common.core.page.TableDataInfo;
import com.guiji.common.utils.PageUtils;
import com.guiji.quartz.mapper.Text2videoConfigMapper;
import com.guiji.quartz.service.IText2videoConfigService;
import com.guiji.quartz.vo.*;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * @program: cms-vue-plus
 * @description:
 * @author: gaoX
 * @date 2021/12/7 10:49
 */
@Service
@DS("slave")
public class Text2videoConfigServiceImpl extends ServicePlusImpl<Text2videoConfigMapper, Text2videoConfig> implements IText2videoConfigService {

	@Override
	public Text2videoConfigVo queryById(Long id){
		return getVoById(id, Text2videoConfigVo.class);
	}

	@Override
	public TableDataInfo<Text2videoConfigVo> queryPageList(Text2videoConfigQueryBo bo) {
		PagePlus<Text2videoConfig, Text2videoConfigVo> result = pageVo(PageUtils.buildPagePlus(), buildQueryWrapper(bo), Text2videoConfigVo.class);
		return PageUtils.buildDataInfo(result);
	}

	@Override
	public List<Text2videoConfigVo> queryList(Text2videoConfigQueryBo bo) {
		return listVo(buildQueryWrapper(bo), Text2videoConfigVo.class);
	}

	private LambdaQueryWrapper<Text2videoConfig> buildQueryWrapper(Text2videoConfigQueryBo bo) {
		Map<String, Object> params = bo.getParams();
		LambdaQueryWrapper<Text2videoConfig> lqw = Wrappers.lambdaQuery();
		lqw.eq(bo.getNewsLang() != null, Text2videoConfig::getNewsLang, bo.getNewsLang());
		lqw.eq(StrUtil.isNotBlank(bo.getNewsCategory()), Text2videoConfig::getNewsCategory, bo.getNewsCategory());
		lqw.eq(bo.getNewsPriority() != null, Text2videoConfig::getNewsPriority, bo.getNewsPriority());
		lqw.eq(StrUtil.isNotBlank(bo.getTtsSpeaker()), Text2videoConfig::getTtsSpeaker, bo.getTtsSpeaker());
		lqw.eq(bo.getTtsRate() != null, Text2videoConfig::getTtsRate, bo.getTtsRate());
		lqw.like(StrUtil.isNotBlank(bo.getDigitalName()), Text2videoConfig::getDigitalName, bo.getDigitalName());
		lqw.eq(StrUtil.isNotBlank(bo.getDigitalHuman()), Text2videoConfig::getDigitalHuman, bo.getDigitalHuman());
		lqw.eq(StrUtil.isNotBlank(bo.getNewsScene()), Text2videoConfig::getNewsScene, bo.getNewsScene());
		lqw.eq(StrUtil.isNotBlank(bo.getTitlePic()), Text2videoConfig::getTitlePic, bo.getTitlePic());
		lqw.eq(StrUtil.isNotBlank(bo.getContentPic()), Text2videoConfig::getContentPic, bo.getContentPic());
		lqw.eq(StrUtil.isNotBlank(bo.getBackup1()), Text2videoConfig::getBackup1, bo.getBackup1());
		lqw.eq(StrUtil.isNotBlank(bo.getBackup2()), Text2videoConfig::getBackup2, bo.getBackup2());
		lqw.eq(StrUtil.isNotBlank(bo.getBackup3()), Text2videoConfig::getBackup3, bo.getBackup3());
		lqw.eq(StrUtil.isNotBlank(bo.getBackup4()), Text2videoConfig::getBackup4, bo.getBackup4());
		lqw.eq(StrUtil.isNotBlank(bo.getBackup5()), Text2videoConfig::getBackup5, bo.getBackup5());
		return lqw;
	}

	@Override
	public Boolean insertByAddBo(Text2videoConfigAddBo bo) {
		Text2videoConfig add = BeanUtil.toBean(bo, Text2videoConfig.class);
		validEntityBeforeSave(add);
		return save(add);
	}

	@Override
	public Boolean updateByEditBo(Text2videoConfigEditBo bo) {
		Text2videoConfig update = BeanUtil.toBean(bo, Text2videoConfig.class);
		validEntityBeforeSave(update);
		return updateById(update);
	}

	/**
	 * 保存前的数据校验
	 *
	 * @param entity 实体类数据
	 */
	private void validEntityBeforeSave(Text2videoConfig entity){
		//TODO 做一些数据校验,如唯一约束
	}

	@Override
	public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
		if(isValid){
			//TODO 做一些业务上的校验,判断是否需要校验
		}
		return removeByIds(ids);
	}
}
